import { io } from 'socket.io-client'
import { notification } from 'ant-design-vue'

export default {
    install(app) {
        const socket = io()

        socket.on('connect', () => {
            console.log('与服务端已建立连接！')

            const token = window.localStorage.getItem('token') 

            socket.emit('user join', token)

            socket.on('auth error', (data) => {
                notification.error({
                    message: '认证错误',
                    description: data.message,
                });
            });

            socket.on('pass certification', (response) => {
                console.log('客户端认证已通过')
            })
        })

        window.addEventListener('beforeunload', () => {
            socket.disconnect()
        });

        app.provide('socket', socket)
    }
}
